<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>我的购物车-小米商城</title>
		<link rel="stylesheet" type="text/css" href="./css/style.css">
		<script src="js/jquery-3.5.1.js" type="application/javascript"></script>
		<script src="js/js.cookie.js" type="application/javascript"></script>
		<script type="text/javascript">
			var a=null;
			$(document).ready(function () {
				$.ajax({
					type: "post",
					dataType: "json",
					url: "GetLoginInfoController",
					success: function (data) {
						if (data.result != "0") {
							a=data.result;
							$("#current_user").empty();
							var str= "<ul>"
									+"<li><a id='current_username' href='self_info' target='_blank'>"+data.result+"</a></li>"
									+"<li>|</li>"
									+"<li><a href='order_center'>订单通知</a></li>"
									+"</ul>";
							$("#current_user").append(str);
						}  else {
							var url = "login";
							Cookies.remove('login_name');
							Cookies.remove('login_pwd');
							location.href = encodeURI(url
									+ "?tip=");
						}
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						// 状态码
						console.log(XMLHttpRequest.status);
						// 状态
						console.log(XMLHttpRequest.readyState);
						// 错误信息
						console.log(textStatus);
					}
				});
				$("#selectAll").click(function () {
					var ck = $(this).prop("checked");
					$("input[name=select]").prop("checked", ck);
				});

			});
			$(function () {
				//获取url参数pageno
				var urlParam = window.location.search;//截取当前url中"?"后面的字符串
				urlParam = decodeURI(urlParam, "UTF-8");//可以用来把字符还原回来
				var params = urlParam.substring(urlParam.indexOf('?') + 1,
						urlParam.length);
				var param1 = params.substring(0, urlParam.indexOf('=') - 1);
				var param1Value = params.substring(urlParam.indexOf('='), urlParam.length);
				pageno = 1;
				if (param1 == "pageno") {
					pageno = param1Value;
				}
			alert("快把车车全都带回家吧！主人");
				$.ajax({
					type: "post",
					dataType: "json",
					url: "cartInfoListController",
					data: {
						"pageno": pageno,
						"username":a
					},
					success: function (msg) {
						//alert(msg.pageno+"  "+msg.maxpage);
						var data = msg.cartinfoList;
						if(a!=null) {
							if (msg.result != "0") {
								//显示列表数据
								$("#show_cart").empty();
								var str = "";
								current_data_counts=data.length;
								$("#totleN").text(current_data_counts);
								for (i in data) {
									str += "<div class='content2 center'>"
											+ "<div class='sub_content fl '>"
											+ "<input type='checkbox' value='quanxuan' class='quanxuan' name='select' id='Oneselect'/>"
											+ "</div>"
											+ "<div class='sub_content fl'><img src='./image/gwc_xiaomi6.jpg'></div>"
											+ "<div class='sub_content fl ft20'>" + data[i].productname + "</div>"
											+ "<div class='sub_content fl '>" + data[i].price + "</div>"
											+ "<div class='sub_content fl'>"
											+ "<input class='shuliang' type='number' value='1' step='1' min='1' onchange='setp(this)'>"
											+ "</div>"
											+ "<div class='sub_content fl'>2499元</div>"
											+ "<div class='sub_content fl'><button value='a' onclick='del("+data[i].cid+")'>x</button></div>"
											+ "<div class='cleas' hidden>"+data[i].cid+"</div>"
											+ "</div>";
								}
								$("#show_cart").append(str);
								//增加分页机制
								var pagination = "<br/><br/><button  id='firstpage' type='button' value='1'  onclick='handlePaganition(this)'>首页</button>&nbsp;&nbsp;&nbsp;&nbsp;"
										+ "<button id='previouspage' type='button' value='1'  onclick='handlePaganition(this)'>上一页</button>&nbsp;&nbsp;&nbsp;&nbsp;"
										+ "<button id='nextpage' type='button' value='1'  onclick='handlePaganition(this)'>下一页</button>&nbsp;&nbsp&nbsp;&nbsp;"
										+ "<button  id='lastpage' type='button' value='1'  onclick='handlePaganition(this)'>尾页</button>&nbsp;&nbsp;&nbsp;&nbsp;";
								$("#pagination").append(pagination);
								$("#previouspage").val(msg.prePage);
								$("#nextpage").val(msg.nextPage);
								$("#lastpage").val(msg.maxPage);
							} else {
								$("#show_cart").empty();
								var str="<div>购物车空空如也！</div>"
								$("#show_cart").append(str);
								$("#totleN").text(0);
							}
						}else {
							var url = "register";
							location.href = encodeURI(url + "?tip=无用户信息，请注册");
						}
					}
				});
			});
			var current_data_counts;
			function handlePaganition(obj) {
				pageno = $(obj).val();
				// alert($("#pagination").html());
				var url = "shopping_cart";

				location.href = encodeURI(url + "?pageno=" + pageno);
			}
			function del(obj) {
				// var productname=$(obj).parent().parent().children("div").get(2).innerHTML;
				// alert(productname);
				$.ajax({
					type:"post",
					dataType:"json",
					url:"DeleteCartinfoController",
					data:{
						"id":obj
					},
					success:function (data) {
						if(data.result=="1"){
							location.href="shopping_cart";
						}
					}
				})
			}
			function setp(obj) {
				var price = $(obj).parent().prev().text();
				var num=$(obj).val();
				var tprice=price*num;
				$(obj).parent().next().text(tprice+"元");
			}
			// function Oneselect(obj) {
			// 	var totleP=parseInt($("#totalP").text());
			// 	var price=parseInt($(obj).parent().next().next().next().next().next().text());
			// 	if($(obj).prop("checked")){
			// 		totleP+=price;
			// 	}
			// 	else
			// 	{
			// 		totleP-=price;
			// 	}
			// 	$("#totalP").text(totleP+"元");
			// }
			function totalPrice() {
				var t = 0;
				var sn=0;
				$("input[name=select]").each(function() {
					if($(this).prop("checked")) {
						//找到小计列中的值进行累加
						var p = $(this).parent().next().next().next().next().next().text();
						t += parseInt(p);
						sn+=1;
					}
					if(sn==current_data_counts){
						$("#selectAll").prop("checked",1);
					}else{
						$("#selectAll").prop("checked",0);
					}
					$("#selectN").text(sn);
					$("#totalP").text(t+"元");
				})
			}
			setInterval(totalPrice,100);
			var ocid="";
			function SettleAccount() {
				$("input:checkbox:checked").each(function () {
					ocid+=$(this).parent().next().next().next().next().next().next().next().text();
					ocid+=" ";
				})
				CartToOrder(ocid);
			}
			function CartToOrder(ocid) {
				var now=new Date();
				var date=now.toLocaleDateString()+""+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
				$.ajax({
					type:"post",
					dataType:"json",
					url:"CartToOrderController",
					traditional:true,
					data:{
						"ocid":ocid,
						"date":date,

					},
					success:function (data) {
						if(data.result=="1"){
							location.href="order_center";
						}
					}
				})
			}
		</script>
	</head>
	<body>
	<!-- start header -->
	<!--end header -->

<!-- start banner_x -->
		<div class="banner_x center">
			<a href="index" target="_blank"><div class="logo fl"></div></a>
			
			<div class="wdgwc fl ml40">我的购物车</div>
			<div class="wxts fl ml20">温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算</div>
			<div class="dlzc fr" id="current_user">
				<ul>
					<li><a href="./login.html" target="_blank">登录</a></li>
					<li>|</li>
					<li><a href="./register.html" target="_blank">注册</a></li>	
				</ul>
				
			</div>
			<div class="clear"></div>
		</div>
		<div class="xiantiao"></div>
		<div class="gwcxqbj">
			<div class="gwcxd center">
				<div class="top2 center">
					<div class="sub_top fl">
						<input type="checkbox" value="quanxuan" class="quanxuan" id="selectAll"/>全选
					</div>
					<div class="sub_top fl">商品名称</div>
					<div class="sub_top fl">单价</div>
					<div class="sub_top fl">数量</div>
					<div class="sub_top fl">小计</div>
					<div class="sub_top fr">操作</div>
					<div class="clear"></div>
				</div>
				<div id="show_cart">
					<div class="content2 center">
						<div class="sub_content fl ">
							<input type="checkbox" value="quanxuan" class="quanxuan" />
						</div>
						<div class="sub_content fl"><img src="./image/gwc_xiaomi6.jpg"></div>
						<div class="sub_content fl ft20">小米6全网通6GB内存+64GB 亮黑色</div>
						<div class="sub_content fl ">2499元</div>
						<div class="sub_content fl">
							<input class="shuliang" type="number" value="1" step="1" min="1" >
						</div>
						<div class="sub_content fl">2499元</div>
						<div class="sub_content fl"><a href="">×</a></div>
						<div class="clear"></div>
					</div>
				</div>
				<div id="pagination"></div>
			</div>
			<div class="jiesuandan mt20 center">
				<div class="tishi fl ml20">
					<ul>
						<li><a href="index">继续购物</a></li>
						<li>|</li>
						<li>共<span id="totleN">2</span>件商品，已选择<span id="selectN">0</span>件</li>
						<div class="clear"></div>
					</ul>
				</div>
				<div class="jiesuan fr">
					<div class="jiesuanjiage fl">合计（不含运费）：<span id="totalP">0</span></div>
					<div class="jsanniu fr"><input class="jsan" type="button" name="jiesuan"  value="去结算" onclick="SettleAccount()"/></div>
					<div class="clear"></div>
				</div>
				<div class="clear"></div>
			</div>
			
		</div>

  

	
	<!-- footer -->
	<footer class="center">
			
			<div class="mt20">小米商城|MIUI|米聊|多看书城|小米路由器|视频电话|小米天猫店|小米淘宝直营店|小米网盟|小米移动|隐私政策|Select Region</div>
			<div>©mi.com 京ICP证110507号 京ICP备10046444号 京公网安备11010802020134号 京网文[2014]0059-0009号</div> 
			<div>违法和不良信息举报电话：185-0130-1238，本网站所列数据，除特殊说明，所有数据均出自我司实验室测试</div>
		</footer>

	</body>
</html>
